package com.gisuni.sddc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.model.ClusterLandInfo;
import com.gisuni.sddc.model.Landexpand;
import com.gisuni.sddc.util.UtilMethod;

@IocBean
public class ClusterDao {

	@Inject
	private Dao dao;  //=OdbcCommon.getGISDao();
	

	public Map queryResultList(String selectSql){
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				 List<Landexpand> list = new ArrayList<Landexpand>();
				 Map map = new HashMap();
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
			    	 Landexpand land = new Landexpand();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
							String distCity = rs.getMetaData().getColumnName(i+1).trim();
//							CITY  DISTRICT  PROJECTTYPE  TYPE   SUM(TOTALAREA)  COUNT(1) TOTALNUM
			    	 	   if (rs.findColumn("CITY") > 0 ){
			    	 		  land.setCity( rs.getString("CITY"));
		    	     	   }
			    	 	   if (rs.findColumn("DISTRICT") > 0 ){
			    	 		  land.setDistrict( rs.getString("DISTRICT"));
		    	     	   }
			    	 	   if (rs.findColumn("PROJECTTYPE") > 0 ){
			    	 		  land.setProjecttype( rs.getString("PROJECTTYPE"));
		    	     	   }
			    	 	   if (rs.findColumn("TYPE") > 0 ){
			    	 		  land.setType( rs.getString("TYPE"));
		    	     	   }
			    	 	   if (rs.findColumn("SUMAREA") > 0 ){
			    	 		  land.setSumArea( rs.getString("SUMAREA"));
		    	     	   }
			    	 	   if (rs.findColumn("SUMCOUNT") > 0 ){
			    	 		  land.setSumCount( rs.getString("SUMCOUNT"));
		    	     	   }
			    	 	   
			    	 	   //
							//行政区
							if("DISTRICT".equals(distCity)){
								if(map.get(obj)!=null){
									int num = (int)map.get(obj) ;
									map.put(obj,++num );
								}else{
									map.put(obj, 1);
								}
							}
							//城市
							if("CITY".equals(distCity)){
								if(map.get(obj)!=null){
									int num =   (int)map.get(obj) ;
									map.put(obj,++num );
								}else{
									map.put(obj, 1);
								}
							}
							
							//TODO 省
							if("PROVINCE".equals(distCity)){
								if(map.get(obj)!=null){
									int num =   (int)map.get(obj) ;
									map.put(obj,++num );
								}else{
									map.put(obj, 1);
								}
							}
							
						}
//			    	 rs.getString("COMPANY")
					 list.add(land);
			    	 result.put(ret);
			    	 map.put("result", list);
			     }
				return map ;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(Map.class);
	}
	
	public Map queryLandResult(String selectSql){
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				Map map = new HashMap();
				Set citySet = new HashSet<String>();
				Set distinctSet = new HashSet<String>();
				List<ClusterLandInfo> list = new ArrayList<ClusterLandInfo>();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				
			     while (rs.next()){
			    	 ClusterLandInfo clusterInfo = new ClusterLandInfo();
			    	 String num = "";
			    	 String landType="";
			    	 String area="";
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
//							Object obj = rs.getString(i + 1);
//							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
				    	 	   if (rs.findColumn("CSMC") > 0 ){
				    	 		  clusterInfo.setCity(rs.getString("CSMC"));
				    	 		  citySet.add(rs.getString("CSMC"));
				    	       }
				    	 	   if (rs.findColumn("FQMC") > 0 ){
				    	 		  clusterInfo.setDistrict(rs.getString("FQMC"));
				    	       }
				    	 	   if (rs.findColumn("LANDTYPE") > 0 ){
				    	 		  landType=rs.getString("LANDTYPE");
				    	       }
				    	 	   if (rs.findColumn("NUM") > 0 ){
				    	 		  num=rs.getString("NUM");
				    	       }
				    	 	   if (rs.findColumn("AREA") > 0 ){
				    	 		  area=rs.getString("AREA");
				    	       }
						}
						if("2".equals(landType)){
							clusterInfo.setGyjcydCount(num);
							clusterInfo.setGyjcydArea(area);
						}else if("7".equals(landType)){
							clusterInfo.setSjjcCount(num);
							clusterInfo.setSjjcArea(area);
						}else if("9".equals(landType)){
							clusterInfo.setSjjtjcCount(num);
							clusterInfo.setSjjtjcArea(area);
						}else if("10".equals(landType)){
							clusterInfo.setCzldCount(num);
							clusterInfo.setCzldArea(area);
						}else if("3".equals(landType)){ //三旧旧城
							clusterInfo.setSjjchCount(num);;
							clusterInfo.setSjjchArea(area);
						}else if("4".equals(landType)){ // 国有鼠辈
							clusterInfo.setGysbydCount(num);
							clusterInfo.setGysbydArea(area);
						}
						distinctSet.add(clusterInfo.getCity()+clusterInfo.getDistrict());
			        list.add(clusterInfo);
			     }
			     map.put("totalData", list);
			     map.put("distinctSet", distinctSet);
			     map.put("citySet", citySet);
				return map;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(Map.class);
	}
	
	public List<Map<String, Object>> queryProjTypesResult(String selectSql){
		return UtilMethod.getFromSql(dao,selectSql);
	}
	
	public String queryNormalResultList(String selectSql){
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
				return result.toString();
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(String.class);
	}
}
